package com.alipay.sign;

import com.alipay.util.AlipaySignature;
import org.apache.commons.codec.binary.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

import static com.alipay.util.AlipayConstants.SIGN_ALGORITHMS;

/**
 * RSA验证
 * Created by 瑱彬 on 2016/8/20.
 */
public class RSA {
    /**
     *签名字符串
     * @param text 需要签名的字符串
     @param sign 签名结果
     @param publicKey 公钥
     @param input_charset 编码格式
     @return 签名结果
     */
    public static  boolean  verify(String text, String sign, String publicKey, String input_charset){
        System.out.println("---------------begin verify RSA------------------");
        System.out.println("text = "+ text);
        System.out.println("sign = "+ sign);
        System.out.println("publicKey = "+ publicKey);
        System.out.println("input_charset = "+ input_charset);
        System.out.println("---------------in verify RSA------------------");
        boolean b = false;
        try {
            b = AlipaySignature.rsaCheckContent(text, sign, publicKey, input_charset);
            System.out.println("签名验证结果："+ b);
        } catch (Exception e) {
            e.printStackTrace();
            return b = false;
        }
        System.out.println("---------------out verify RSA------------------");
        return b;
    }
    public static String sign(String content,String rsaPrivate) {
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(rsaPrivate));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);

            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);

            signature.initSign(priKey);
            signature.update(content.getBytes("utf-8"));

            byte[] signed = signature.sign();

            return Base64.encodeBase64String(signed);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String rsaContent = "body=太极虎私教端-支付宝充值&buyer_email=23006239@qq.com&buyer_id=2088202" +
                "855632892&discount=0.00&gmt_create=2016-09-18 14:16:51&is_total_fee_adjust=Y&not" +
                "ify_id=62ee0b6fe87b0cf4f4235345f139ad6mva&notify_time=2016-09-18 14:31:29&notify" +
                "_type=trade_status_sync&out_trade_no=201609180216430089602&payment_type=1&price=" +
                "0.01&quantity=1&seller_email=andre@pleasurehk.com&seller_id=2088021578709731&sub" +
                "ject=充值&total_fee=0.01&trade_no=2016091821001004890295701466&trade_status=WAIT" +
                "_BUYER_PAY&use_coupon=N";
//        String rsaContent = "body=回调&buyer_email=714276725@qq.com&buyer_id=2088602183969242&discount=0.00&gmt_create=2016-08-26 11:11:31&is_total_fee_adjust=Y&notify_id=daa66df84f0e2da50695053ed46abbdhuq&notify_time=2016-08-26 11:11:31&notify_type=trade_status_sync&out_trade_no=ii20160826102605282&payment_type=1&price=0.02&quantity=1&seller_email=604543538@qq.com&seller_id=2088321037033185&subject=拼设备&total_fee=0.02&trade_no=2016082621001004240291359071&trade_status=WAIT_BUYER_PAY&use_coupon=N";
        String sign = "PVr9MaNhqUmfQgnogGIKBJeRR+Mw47p896xYi4L1OB7w+SNYDs7M+ZkowotWh0qA/bk3ExUT+" +
                "nIHZ6UqFf75zm/2aakOndegtPHvzoJwkqAIQPG+25TAIcdrcsh2qmbBkuzB7nvUVysRrDLxaP4ZsTovX" +
                "E1TkkF21/cg9ICzM7I=";
//        String sign = "LxnWpFbN8Buu2M++7AL4cMzaZy1ClKrWKxz0JvB4upQFfb5s8qNpmE/QDje7R67NhjWOp6cBQAz7gFDn80uduuf6rxtlcAxLtkOOB2wbZVsYKpnabfmcSQlZHf2kqC/Y6cDhq3pUu8LDS2XLzth9svSp2Jp4eRfqlBICI9ftXnc=";
//        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
        boolean verify = verify(rsaContent, sign, publicKey, "utf-8");
        System.out.println(verify);

    }
}
